<template>
  <div class="app-container">
    <div v-show="showIndexList">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="合同编号" prop="contractNo">
          <el-input
            v-model="queryParams.contractNo"
            placeholder="请输入合同编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerId">
          <el-select v-model="queryParams.customerId" filterable clearable  placeholder="请选择客户信息">
              <el-option
                  v-for="dict in customerList"
                  :key="dict.customerId"
                  :label="dict.customerName"
                  :value="dict.customerId"
                  />
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="付款方式" prop="paymentType">
            <el-select v-model="queryParams.paymentType" placeholder="请选择付款方式" clearable size="small">
              <el-option
                    v-for="dict in paymentTypeOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  />
            </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <!-- <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['business:cost:export']"
          >导出</el-button>
        </el-col> -->
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="contractDeliveryList">
        <el-table-column label="合同编号" align="center" prop="contractNo" fixed width="150"/>
        <el-table-column label="客户名称" align="center" prop="customerName" v-if="columns[0].visible" />
        <el-table-column label="付款方式" align="center" prop="paymentType"  v-if="columns[1].visible" :formatter="paymentTypeFormat" min-width="300"/>
        <el-table-column label="负责人" align="center" prop="director" v-if="columns[2].visible" />
        <el-table-column label="合同金额" align="center" prop="contractSum" v-if="columns[3].visible" :formatter="numberFormat"/>
        <el-table-column label="交货日期" align="center" prop="deliveryDate" v-if="columns[4].visible" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="完工进度" align="center" prop="completeType" v-if="columns[5].visible" :formatter="completeTypeFormat"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >配送维护</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <DeliveryDetail v-if="!showIndexList" @onDetailBack="detailBack" :editConfig="editConfig"></DeliveryDetail>
  </div>
</template>

<script>
import { listContractDelivery, exportCost } from "@/api/business/delivery";
import { listCustomer} from "@/api/business/customer";
import DeliveryDetail from './detail'

export default {
  name: "Cost",
  components:{
    DeliveryDetail
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同交货表格数据
      contractDeliveryList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNo: null,
        customerId: null,
        paymentType: null,
      },
      // 是否显示index页面
      showIndexList:true,
      customerList:[],
      // 付款方式数据字典
      paymentTypeOptions: [],
      // 完工进度数据字典
      completeTypeOptions: [],
      editConfig: {
        id: '',
        canEdit: true
      },
      // 列信息
      columns: []
    };
  },
  created() {
    this.getDicts("bus_payment_type").then(response => {
      this.paymentTypeOptions = response.data;
    });
    this.getDicts("bus_complete_type").then(response => {
      this.completeTypeOptions = response.data;
    });
    this.loadColumns();
    this.getList();
    this.getCustomerList();
  },
  methods: {
    numberFormat(row, column, cellValue, index){
        return cellValue.toLocaleString();
    },
    // 菜单状态字典翻译
    paymentTypeFormat(row, column) {
      return this.selectDictLabel(this.paymentTypeOptions, row.paymentType);
    },
    // 菜单状态字典翻译
    completeTypeFormat(row, column) {
      return this.selectDictLabel(this.completeTypeOptions, row.completeType);
    },
    // 查询客户信息列表
    getCustomerList() {
      let queryParams= {
        pageNum: 1,
        pageSize: 999,
        customerName: null,
        paymentType: null,
      }

      listCustomer(queryParams).then(response => {
        this.customerList = response.rows;
      });
    },
    /** 查询合同成本列表 */
    getList() {
      this.loading = true;
      listContractDelivery(this.queryParams).then(response => {
        this.contractDeliveryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editConfig.id = row.contractId
      this.showIndexList = false
    },
    
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同交货数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCost(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    detailBack(val) {
      this.showIndexList = true
      this.getList()
    },
    getDefaultColumns(){
      let columns =[
        { key: 0, label: `客户名称`, visible: true },
        { key: 1, label: `付款方式`, visible: false },
        { key: 2, label: `负责人`, visible: true },
        { key: 3, label: `合同金额`, visible: true },
        { key: 4, label: `交货日期`, visible: true },
        { key: 5, label: `完工进度`, visible: true }
      ];
      return columns;
    },
    loadColumns(){
      let customerColumns = window.localStorage.getItem("delivery-columns")
      let columns = this.getDefaultColumns()
      if (customerColumns != null){
          columns = JSON.parse(customerColumns)
      }
      this.columns = columns;
    }
  },
  watch: {
    columns:{
      handler(value){
        let str = JSON.stringify(value);
        window.localStorage.setItem("delivery-columns",str)
      },
      deep: true
    }
  }
};
</script>
